{
    /**
     * The slice() method of Array instances returns
     *   a shallow copy of a portion of an array into a new array object
     *     selected from start to end (end not included)
     *       where start and end represent the index of items in that array.
     *   The original array will not be modified.
     */
    console.log('java.script'.slice(1))

    const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

    console.log(animals.slice(2));     // => ["camel", "duck", "elephant"]
    console.log(animals.slice(2, 4));  // => ["camel", "duck"]
    console.log(animals.slice(1, 5));  // =>  ["bison", "camel", "duck", "elephant"]
    console.log(animals.slice(-2));    // =>  ["duck", "elephant"]
    console.log(animals.slice(2, -1)); // => ["camel", "duck"]
    console.log(animals.slice());      // => ["ant", "bison", "camel", "duck", "elephant"]
}

{ // prototype.push(), 注意 push() 所添加的元素的位置，及其返回值

    const animals = ['pigs', 'goats', 'sheep'];

    const count = animals.push('cows');
    console.log(count);  // => 4

    console.log(animals); // => ["pigs", "goats", "sheep", "cows"]
}

{ // prototype.pop(), 注意 pop() 所弹出的是哪个元素（第一个，还是最后一个）？它的返回值？
    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];

    console.log(plants.pop()); // => "tomato"
    console.log(plants); // => ["broccoli", "cauliflower", "cabbage", "kale"]

    plants.pop();
    console.log(plants); // => ["broccoli", "cauliflower", "cabbage"]
}

{
    /**
     *  prototype.shift()
     *    removes the first element from an array and returns that removed element.
     */

    const array1 = [1, 2, 3];

    const firstElement = array1.shift();

    console.log(array1); // => [2, 3]

    console.log(firstElement); // => 1
}

{
    /**
     * The unshift() method of Array instances adds
     *   the specified elements to the beginning of an array
     *   and returns the new length of the array.
     */
    const array1 = [1, 2, 3];

    console.log(array1.unshift(4, 5)); // => 5

    console.log(array1); // => Array [4, 5, 1, 2, 3]
}

{
    /**
     * The reduce() method of Array instances executes
     *   a user-supplied "reducer" callback function on each element of the array,
     *   in order, passing in the return value from the calculation on the preceding element.
     */
    const array1 = [0, 1, 2, 3, 4]
    const initialValue = 0;
    const sumWithInitial = array1.reduce(
        (accumulator, currentValue) => accumulator + currentValue,
        initialValue,
    );

    console.log(sumWithInitial); // => 10
}

{
    const array1 = ['one', 'two', 'three'];
    console.log('array1:', array1);  // => "array1:" Array ["one", "two", "three"]

    const reversed = array1.reverse();
    console.log('reversed:', reversed); // => "reversed:" Array ["three", "two", "one"]

    // Careful: reverse is destructive -- it changes the original array.
    console.log('array1:', array1);  // => "array1:" Array ["three", "two", "one"]
}

{
    let a = [1, 2, 3]

    let c = a.map(e => e + 1)
    console.log(c) // => [2, 3, 4]

    a.forEach(e => e + 1) // return `void`
    console.log(a) // => `a` was not changed, still [1, 2, 3]

    a.forEach((_, i) => a[i]++)
    console.log(a) // => [2, 3, 4]
}

{
    console.log(Array.isArray([1, 3, 5]));   // => true
    console.log(Array.isArray('[]'));            // => false
    console.log(Array.isArray(new Array(5)));   // => true
}
